package jp.ne.iforce.android.jsendtest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class C2dmSend{

	/**
	 * C2DM登録情報
	 * account：iforce.develop@gmail.com
	 * package：jp.ne.iforce.android.jschatest
	 *
	 * AuthToken取得URL
	 * https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&service=ac2dm&Email=iforce.develop&Passwd=iforce.ne.jp&source=jp.ne.iforce.android.jschatest
	 */

	private String SEND_URL = "https://android.apis.google.com/c2dm/send";
	private String AUTHORZATION = "GoogleLogin auth=DQAAAL0AAAAuUWveKChhCHhNdtsJZ2xAVgdiZUoL6pSMG48_PSBTqI6PbbVLtU7bZc-VEr4fa7sGpgYKiqRfJz_pL6RyNSwEvwAQcpUL-O5g4iJg6YzdXaWd8AN7Ef6quBAKec9-K582JlgEevQDhXAtkQu1QLs9O6iJw_ExZ8AztCE29nCHGS7uu4JhKktjtlsSAlWBDEjWamYFIVAfzRe_Ycpu819EB9TbTWN0nVcSqAkTSyUgZKzPGZWEmaMuD2iXDtO-ats";

	//for SO-01C(熊川)
	private String REGISTRATION_ID = "APA91bF1zuNRyxmExmhBesVzAuFpT1obQOXYZkMyg6cDliZVPYKZOR0GJz3zqfOygkuXsxDG1LmRpD-brh1A2kF9iivcV5t3nPmqO05sH8xY1JW4iIsh82SwKBrDUNoMPFwXN9i_mor62as4ByPhC8Rg-BzLbUXYGmUrLEfdcdGmIH6YiJtkWMs";

	public String message;
	public String Tsunami;
	public String Level;
	public String Sec;

	public String SendMessage(String type){

    	String resStr = "";

    	ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("registration_id", REGISTRATION_ID));
    	params.add(new BasicNameValuePair("collapse_key", "1"));
    	params.add(new BasicNameValuePair("data.type", type));
    	if(type.equals("message")){
        	params.add(new BasicNameValuePair("data.message", message));
    	}else if(type.equals("earthquake")){
    		params.add(new BasicNameValuePair("data.level", Level));
    		params.add(new BasicNameValuePair("data.sec", Sec));
    		params.add(new BasicNameValuePair("data.tsunami",Tsunami));
        }


    	HttpPost httpPost = new HttpPost(SEND_URL);

    	try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");// ヘッダを設定
			httpPost.setHeader("Authorization ", AUTHORZATION);// ヘッダを設定

			DefaultHttpClient client = new DefaultHttpClient();
	    	HttpResponse httpResponse = client.execute(httpPost);

	    	int statusCode = httpResponse.getStatusLine().getStatusCode();

	    	HttpEntity entity = httpResponse.getEntity();
	    	String response = EntityUtils.toString(entity);

	    	entity.consumeContent();

	    	client.getConnectionManager().shutdown();

	    	resStr = statusCode +":"+response;

		} catch (UnsupportedEncodingException e1) {
			// TODO 自動生成された catch ブロック
			e1.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

		return resStr;

	}
}